package com.learning.spring.bean.java.config;

import com.learning.spring.bean.java.config.entity.Address;
import com.learning.spring.bean.java.config.entity.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

/**
 * ClassName: SpringContextConfig
 * Description: 使用 Java类提供Bean定义信息
 * 1. 普通的POJO 只要标注了 @Configuration 注解，就可以为Spring 容器提供Bean定义的信息
 * 2. 每个标注了 @Bean 的类方法都相当于提供了一个Bean的定义信息
 * Date: 2020/8/21 20:31 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
@Configuration
@ComponentScan(basePackages = "com.learning.spring.bean.java.config")
@ImportResource(value = "classpath:spring-context.xml")
public class SpringContextConfig {

    @Bean
    public User userBeanJavaConfig() {
        User user = new User();
        user.setName("sam");
        user.setAge(30);
        // 调用 @Configuration 标注类方法时，Spring 框架会使用AOP增强，不是简单的JVM方法调动，而是会从容器中 getBean
        user.setAddress(addressBeanJavaConfig());
        return user;
    }

    /**
     * 指定Bean的名称
     *
     * @return
     */
    @Bean(value = "addressBeanJavaConfig")
    public Address addressBeanJavaConfig() {
        Address address = new Address();
        address.setName("上海");
        return address;
    }
}
